FROM docker.m.daocloud.io/library/nginx:latest

# 设置环境变量, 防止出现选择时区
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# 使用国内源文件
COPY sources.list /etc/apt/sources.list

RUN echo "====> 同步时间" && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    \
    apt-get update && \
    apt install -y --no-install-recommends \
        cron \
        logrotate \
        --reinstall ca-certificates && \
        update-ca-certificates -f && \

    \
    echo "====> Clear Up" && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 复制配置文件 logrotate
RUN rm -rf /etc/logrotate.d/*

COPY logrotate.d/nginx/daily /etc/logrotate.d/nginx/daily
RUN echo "====> 给logrotate配置644权限" && \
    chmod 644 /etc/logrotate.d/nginx/daily && \

    echo "====> 添加计划任务" && \
    echo "59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx/daily >/dev/null 2>&1" > /var/spool/cron/crontabs/root && \
    chmod 600 /var/spool/cron/crontabs/root

# 复制运行脚本 启动cron supervisord
#COPY init.sh /usr/local/bin/init.sh
RUN touch /usr/local/bin/init.sh && \
    echo "#!/bin/sh" > /usr/local/bin/init.sh && \
    echo "echo \"try start cron\";" >> /usr/local/bin/init.sh && \
    echo "service cron start;" >> /usr/local/bin/init.sh && \
    echo "nginx -g 'daemon off;';" >> /usr/local/bin/init.sh

RUN echo "====> 给启动脚本增加运行权限" && \
    chmod +x /usr/local/bin/init.sh

CMD ["/usr/local/bin/init.sh"]
